# Usage:
#
# make binary = Make binary file.
#
# make eeprom = Make eeprom file.
#
# make clean = Clean object files.
#
# make clean-deps = Clean object files and dependencies.
#
# make program = Download the hex file to the device

APP=m2m_client

#Directories
SOURCE_DIR = ../../FreeRTOS/Source
PLATFORM_DIR=../../Platform/nano
PORT_DIR = $(PLATFORM_DIR)
COMMON_DIR =../../Common

# Application options
include app.rules
# Platform options
include $(PLATFORM_DIR)/platform.rules

# Target file name (without extension).
TARGET = $(APP)

# Optimization level
OPT =
#OPT = s

# C sources
SRC +=
# $(PLATFORM_DIR)/uart.c 

# Additional build options
#CFLAGS += -DCC2430_EM

# Assembler source files
ASRC += 


# Extra include directories
EXTRAINCDIRS = 

CFLAGS +=

# ---------------------------------------------------------------------------

# Object files.
#OBJS = $(notdir $(SRC) ) $(notdir $(ASRC) )
OBJS = $(addprefix .rels/build/$(APP)/,$(SRC:.c=.rel) ) $(addprefix .rels/build/$(APP)/,$(ASRC:.S=.rel) )
DEPS = $(addprefix .deps/deps/$(APP)/,$(SRC:.c=.d) ) 
# Define all listing files.
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)

# Finish command line options
ALL_CFLAGS = $(CFLAGS) -I.
ALL_ASFLAGS = $(ASFLAGS)  -I. -x assembler-with-cpp 

# Build targets:
binary: link $(TARGET).$(FORMAT)
	@echo ""
	@echo "--- Build complete ---"

config_start: 
	@echo "--- Config ---"

config: config_start module_id.h modules_conf.h
	@echo "--- Config done ---"

depend_start:
	@echo "--- Depend ---"
	
.deps/depend: 
	@mkdir -p .deps
	@touch .deps/depend

dep:	 module_id.h modules_conf.h
	@echo "--- Depend done ---"

obj: dep $(OBJS) 
	@echo ""
	@echo "--- Build done ---"

$(TARGET).hex: $(OBJS)
	$(CC) $(LDFLAGS) -o $(TARGET).hex $(OBJS) 
	
link: obj $(TARGET).$(FORMAT)
	@echo ""
	@echo "--- Link done ---"

clean:
	rm -rf .rels $(TARGET).hex *.hex *.asm *.ihx *.lnk *.lst *.map *.mem *.rel *.rst *.sym modules_conf.h module_id.h

clean-deps: 
	rm -rf .deps 
	
.deps/deps/$(APP):
	mkdir -p .deps/deps/$(APP)

.rels/build/$(APP):
	mkdir -p .rels/build/$(APP)
	
.rels/build/$(APP)/%.rel: %.c
	@echo
	@echo "Compile:" $<
	@mkdir -p $(dir  $(addprefix .rels/build/$(APP)/, $(<:.c=.rel)) ) 
	$(CC) -c $(ALL_CFLAGS) $< -o $(addprefix .rels/build/$(APP)/, $(<:.c=.rel)) 

.rels/build/$(APP)/%.s : %.c
	@echo
	@echo "Compile to asm:" $<
	$(CC) -S $(ALL_CFLAGS) $< -o $(addprefix .rels/build/$(APP)/, $(<:.c=.s)) 

.rels/build/$(APP)/%.rel : %.S
	@echo
	@echo "Assemble:" $<
	$(CC) -c $(ALL_ASFLAGS) $< -o $(addprefix .rels/build/$(APP)/, $(<:.S=.rel)) 

.deps/deps/$(APP)/%.d: %.c
	@echo "Depend:" $<
	@mkdir -p $(dir  $(addprefix .deps/deps/$(APP)/, $(<:.c=.d)) ) 
	@$(CC) -MM $(ALL_CFLAGS) -o .deps/tmp $<
	@echo -n .rels/build/$(APP)/$(dir $<) > $@
	@cat .deps/tmp >> $@

%.sym: %.elf
	@echo
	@echo "Symbols:" $@
	$(NM) -n $< > $@

%.lss: %.elf
	@echo
	@echo "Listing:" $@
	$(OBJDUMP) -h -S $< > $@
	
program: binary
	../../Tools/nano_programmer/nano_programmer -d $(BSL_PORT) --program $(TARGET).$(FORMAT)
		 
include .deps/depend
